package com.ransj.iphone.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * IFloat View
 * 
 * @author android
 * 
 */
public class ILinearLayout extends LinearLayout {
	private static final int FILTER_DISTANCE = 20;
	private GestureDetector mDetector;
	private MyOnTouchListener mTouchListener;
	private MyGestureListener mGestureListener;
	private boolean mIsCanMove;
	private boolean mIsMove;
	private OnClickListener mOnClickListener;

	public ILinearLayout(Context context) {
		super(context);
		init();
	}

	public ILinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ILinearLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	@Override
	public void setOnClickListener(OnClickListener l) {
		mOnClickListener = l;
	}
	
	public void setSuperOnClickListener(OnClickListener l) {
		super.setOnClickListener(l);
	}

	private void init() {
		mGestureListener = new MyGestureListener();
		mDetector = new GestureDetector(mGestureListener);
	}

	public void setMyOnTouchListener(MyOnTouchListener listener) {
		mTouchListener = listener;
	}

	public void setMoveable(boolean moveable) {
		mIsCanMove = moveable;
		if(!moveable){
			setSuperOnClickListener(mOnClickListener);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			mGestureListener.onUp(event);
			mDetector.onTouchEvent(event);
		} else if (mIsCanMove) {
			mDetector.onTouchEvent(event);
		}
		return super.onTouchEvent(event);
	}

	private class MyGestureListener extends
			GestureDetector.SimpleOnGestureListener {

		public boolean onUp(MotionEvent e) {
			if (mTouchListener != null) {
				return mTouchListener.onUp(e);
			}
			return false;
		}

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			if (mTouchListener != null) {
				return mTouchListener.onDoubleTap(e);
			}
			return false;
		}
		
		@Override
		public void onLongPress(MotionEvent e) {
//			if (!mIsMove && mTouchListener != null) {
//				mTouchListener.onLongPress(e);
//			}
		}

		@Override
		public boolean onDown(MotionEvent e) {
			mIsMove = false;
			ILinearLayout.this.setSuperOnClickListener(mOnClickListener);
			if (mTouchListener != null) {
				return mTouchListener.onDown(e);
			}
			return false;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			mIsMove = true;
			ILinearLayout.this.setSuperOnClickListener(null);
			if (mTouchListener != null) {
				return mTouchListener.onFling(e1, e2, velocityX, velocityY);
			}
			return false;
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			if (Math.abs(distanceX) <= FILTER_DISTANCE
					&& Math.abs(distanceY) <= FILTER_DISTANCE) {
				mIsMove = false;
			} else {
				mIsMove = true;
				ILinearLayout.this.setSuperOnClickListener(null);
			}
			if (mTouchListener != null) {
				return mTouchListener.onScroll(e1, e2, distanceX, distanceY);
			}
			return false;
		}

	}

	public interface MyOnTouchListener {
		public boolean onUp(MotionEvent e);

		public boolean onDoubleTap(MotionEvent e);
		
		public void onLongPress(MotionEvent e);

		public boolean onDown(MotionEvent e);

		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY);

		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY);
	}

}
